// pages/index/gooddaily/list.js
//获取工具类的应用实例  
const util = require('../../../utils/util.js');
const api = require('../../../utils/api.js');

const App = getApp();

Page({
  data: {

    count: 0,

    //页面适配
    imgwidth: 0,
    imgheight: 0,
    windowWidth: wx.getSystemInfoSync().screenWidth,
    windowHeight: wx.getSystemInfoSync().windowHeight,
    //wxml渲染层参数
    goods_data: {
      list: [],
      //page: {
      //  page_no: 0,
      //  page_size: 6,
      //},
      //paginate: {}
    }
  },

  //初始化数据加载
  onLoad() {
    // this.getGoodDailyList();
    this.onPullDownRefresh()
    
  },



  //加载每日推荐列表分页
  getGoodDailyList() {
    const self = this;

    /* 
        let pageData = {
          page_no: this.data.goods_data.page.page_no + 1,
          page_size: this.data.goods_data.page.page_size,
        }
     */

    this.data.count++;

    console.log("KKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKK     " + this.data.count)
  

    let data = {
      //page: JSON.stringify(pageData),
      service: 'psGoods.smallprogram.freeGetGoodsActivityHomeListPageFind',
      params: '{}',

      url_type: 'goods',
      app_token: App.globalData.app_token,        
      security_code: App.globalData.security_code
    }


    var goods_data = this.data.goods_data

    api.reqData({
      data,
      success: (res) => {
        console.log(res.data.data)
        var docList = res.data.data.doc_url
        var itemList = res.data.data.list
        var doc_url_key = Object.keys(docList)
        for (var i = 0; i < itemList.length; i++) {
          JSON.parse(itemList[i].pic_info)
          var path_id = JSON.parse(itemList[i].pic_info)[0].path_id
          itemList[i].taobao_link = (itemList[i].market_price - itemList[i].discount_price).toFixed(2);

          if (path_id != '') {
            for (var e = 0; e < doc_url_key.length; e++) {
              if (path_id === doc_url_key[e]) {
                itemList[i].pic_src = docList[doc_url_key[e]].replace("https", "http")
              }
            }
            if (path_id == '643e4547fc5b46e38d6af33390649c32' || path_id == 'a5159783b7f7402eab3bfcb9fcbecdfd'){
              itemList.splice(i--, 1);
             }
          } else {
            itemList[i].pic_src = ''
          }
          
        }
        

        goods_data.list = itemList

        //goods_data.page.page_no = res.data.data.page.page_no
        //goods_data.page.page_size = res.data.data.page.page_size
        //paginate.total_page = res.data.data.page.total_page
        //goods_data.paginate.total_count = res.data.data.page.total_count
        //判断hasNext

        /* 
        if (goods_data.paginate.total_count > goods_data.page.page_no * goods_data.page.page_size) {
          console.log("后面有数据 还没有查询完")
          goods_data.paginate.hasNext = true
        } else {
          console.log("后面没有数据了，不要再拉了")
          goods_data.paginate.hasNext = false
        }
         */
        this.setData({
          goods_data: goods_data,
        })
      }
    })
  },


  //拉到底部刷新
  onPullDownRefresh() {
    console.info('onPullDownRefresh')
    //if (!this.data.goods_data.paginate.hasNext) return
    //this.getGoodDailyList()
    
    console.log("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!")

    wx.showLoading({
      title: '加载中',
    })


    setTimeout(function () {
      wx.hideLoading()
      wx.hideNavigationBarLoading() //完成停止加载
      wx.stopPullDownRefresh() //停止下拉刷新
    }, 1500)

    //this.initData()
    this.getGoodDailyList()
  },

  toDetail: function (e) {

    let source = e.currentTarget.dataset.source;
    let goods_id = e.currentTarget.id;

    if (source == 'meitianhui') {
      wx.navigateTo({
        url: `../../buy/detail/selfEmployed/selfEmployed?goods_id=` + goods_id,
      });
    } else {
      wx.navigateTo({
        url: `../../buy/detail/taotaoling/taotaoling?goods_id=` + goods_id,
      });
    }

  },


  //商品图片适配
  imageLoad: function (e) {
    var windowWidth = this.data.windowWidth;
    //新人专享列表查询图片宽高：589 855
    var $width = e.detail.width;

    //设置图片显示宽度,适配0.92的比例，
    var viewWidth = windowWidth;
    var viewHeight = viewWidth;    //计算的高度值     
    this.setData({
      imgwidth: viewWidth,
      imgheight: viewHeight
    })
  },

  /**
 * 页面上拉事件的处理函数
 */
  onReachBottom() {
    console.info('onReachBottom')

/*     if (!this.data.goods_data.paginate.hasNext) {
      console.log("到底咯！！！！！！！！！！！！！！！！！！")
      return
    } */
    this.getGoodDailyList()

  },

})